package com.qidian.mes.pro.service.impl;

import com.qidian.mes.pro.mapper.ProcessMapper;
import com.qidian.mes.pro.service.ProcessService;
import com.qidian.mes.util.dot.PagerDTO;
import com.qidian.mes.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qidian.mes.pojo.Process;

import java.util.List;

/**
 * @Author dqy
 * @Description
 * @Time 2024/4/7 16:09
 */
@Service
public class ProcessServiceImpl implements ProcessService {
    @Autowired
    private ProcessMapper processMapper;
    @Override
    public QdResult saveProcess(Process process) {
        if (processMapper.selectByCode(process.getProcessCode())!=null)
            return QdResult.duplicate("工序编码已存在");
        if (processMapper.selectByName(process.getProcessName())!=null)
            return QdResult.duplicate("工序名称已存在");
        return QdResult.isSuccess(processMapper.insertProcess(process));
    }

    @Override
    public QdResult<List<Process>> queryAll() {
        return QdResult.success(processMapper.queryAll());
    }

    @Override
    public QdResult<PagerDTO<Process>> queryByPage(Process process, int start, int limit) {
        PagerDTO<Process> pagerDTO = new PagerDTO<>();
        pagerDTO.setPage(start);
        pagerDTO.setLimit(limit);
        pagerDTO.setTotal(processMapper.selectCount(process));
        pagerDTO.setData(processMapper.queryByPage(process,(start-1)*limit,limit));
        return QdResult.success(pagerDTO);
    }

    @Override
    public QdResult deleteById(Long processId) {
        processMapper.deleteById(processId);
        return QdResult.success();
    }

    @Override
    public QdResult deletes(Long[] processIds) {
        processMapper.deleteByIds(processIds);
        return QdResult.success();
    }

    @Override
    public QdResult updateProcess(Process process) {
        if (process.getProcessId() == null){
            return QdResult.error("没有对应Id");
        }
        Process tempProcess = processMapper.selectByCode(process.getProcessCode());
        if (tempProcess!=null && !tempProcess.getProcessId().equals(process.getProcessId())){
            return QdResult.duplicate("工序编码已存在");
        }
        tempProcess = processMapper.selectByName(process.getProcessName());
        if (tempProcess!=null && !tempProcess.getProcessId().equals(process.getProcessId())){
            return QdResult.duplicate("工序名称已存在");
        }
        return QdResult.isSuccess(processMapper.updateProcess(process));
    }

    @Override
    public QdResult queryById(Long processId) {
        return QdResult.success(processMapper.selectById(processId));
    }
}
